package com.xrtn.controller;

import com.xrtn.bean.InitAdvert;
import com.xrtn.bean.SysUserInfo;
import com.xrtn.bean.TReplyInfo;
import com.xrtn.bean.TSubclinicInfo;
import com.xrtn.service.ICommonService;
import com.xrtn.service.ISysAdService;
import com.xrtn.service.ISysUserService;
import com.xrtn.utils.CodeEnum;
import com.xrtn.utils.MD5Utils;
import com.xrtn.utils.ObjUtils;
import com.xrtn.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by TreeJohn on 2018/3/2.
 */

@Api("员工相关接口")
@Controller
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/sysuser")
public class SysUserController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ICommonService commonService;

    /**
     * 进入员工管理页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoCtaffManagement", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoCtaffManagement(ModelAndView modelAndView) {
        modelAndView.setViewName("staffManagement/staffManagement_hp");
        return modelAndView;
    }
    /**
     * 进入员工绩效管理页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoCtaffManagementDetail", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoCtaffManagementDetail(ModelAndView modelAndView) {
        modelAndView.setViewName("staffManagement/staffAchievementsDetail");
        return modelAndView;
    }
    /**
     * 进入员工评价详情页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoCtaffStarDetail", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoCtaffStarDetail(ModelAndView modelAndView) {
        modelAndView.setViewName("staffManagement/staffStarDetail");
        return modelAndView;
    }
    /**
     * 进入离职员工管理页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoCtaffManagement_q", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoCtaffManagement_q(ModelAndView modelAndView) {
        modelAndView.setViewName("staffManagement/staffManagement_q");
        return modelAndView;
    }
    /**
     * 进入员工管理页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoAddSysUser", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoAddSysUser(ModelAndView modelAndView) {
        modelAndView.setViewName("sysUserAdd");
        Map<String,Object> params = new HashMap<>();
        List<SysUserInfo> userList = commonService.selectSysUserList(params);

        Integer userNumber = commonService.selectUserMaxNo();

        String userNo = ObjUtils.getUserNo(userNumber+"");

//        System.out.println("userNo="+userNo);

        modelAndView.addObject("userNo", userNo);

        return modelAndView;
    }
    /**
     * 进入员工绩效
     *
     * @return
     */
    @RequestMapping(value = "/gotoSysUserAchievements", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoSysUserAchievements(ModelAndView modelAndView) {
        modelAndView.setViewName("staffManagement/staffAchievements");
        return modelAndView;
    }
    /**
     * 员工详情
     * @return
     */
    @RequestMapping(value = "/gotoCtaffManagementInfo", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoCtaffManagementInfo(ModelAndView modelAndView,Integer userId) {
        modelAndView.setViewName("staffManagement/staffContent");
        modelAndView.addObject("userId",userId);
        return modelAndView;
    }

    /**
     * 评分详情
     * @return
     */
    @RequestMapping(value = "/gotoSysUserReplyInfo", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoSysUserReplyInfo(ModelAndView modelAndView,Integer userId) {
        modelAndView.setViewName("staffManagement/staffStarDetail");
        modelAndView.addObject("userId",userId);
        return modelAndView;
    }
    /**
     * 添加员工
     *
     * @param sysUserInfo
     * @return
     */
    @ApiOperation(value = "添加员工", notes = "添加员工")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userNo", dataType = "string", required = true, value = "员工编号，登录用", defaultValue = "GYT000002"),
            @ApiImplicitParam(paramType = "query", name = "userName", dataType = "string", required = true, value = "用户真实姓名", defaultValue = "张三丰"),
            @ApiImplicitParam(paramType = "query", name = "subclinicId", dataType = "int", required = true, value = "所属分店ID", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "subclinicName", dataType = "string", required = true, value = "所属分店名称", defaultValue = "顺义分店"),
            @ApiImplicitParam(paramType = "query", name = "userImg", dataType = "string", required = false, value = "职业照", defaultValue = "https://t1.onvshen.com:85/gallery/17596/24921/0.jpg"),
            @ApiImplicitParam(paramType = "query", name = "userRole", dataType = "int", required = true, value = "用户角色/职位", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "userPhone", dataType = "string", required = false, value = "联系电话", defaultValue = "13312341234"),
            @ApiImplicitParam(paramType = "query", name = "userGender", dataType = "int", required = false, value = "用户性别:0:男;1:女", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "userSource", dataType = "int", required = false, value = "员工来源", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "userTitle", dataType = "string", required = false, value = "职称", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "workYear", dataType = "string", required = false, value = "十五年服务经验", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "levelType", dataType = "string", required = false, value = "评价", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "expertDesc", dataType = "string", required = false, value = "擅长领域", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "workExperience", dataType = "string", required = false, value = "工作经验", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "parentsSound", dataType = "string", required = false, value = "家长心声", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "learnResult", dataType = "string", required = false, value = "学术成果", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "learnResultImg", dataType = "string", required = false, value = "学术成果图片", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "proAuth", dataType = "string", required = false, value = "专业认证", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "proAuthImg", dataType = "string", required = false, value = "专业认证图片", defaultValue = "1"),
    })
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData addSysUser(SysUserInfo sysUserInfo) {

        ResponseData data = new ResponseData();

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{sysUserInfo.getUserNo(), sysUserInfo.getUserName(),
                sysUserInfo.getSubclinicId(), sysUserInfo.getSubclinicName(), sysUserInfo.getUserRole()});

        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        }

        sysUserInfo.setCreateTime(new Date());
        sysUserInfo.setUserPwd(MD5Utils.md5Password("123456")); // 默认密码


        int count = userService.insertSysUser(sysUserInfo);
        if (count < 0) {
            if (count == -1) {
                data.setCodeEnum(CodeEnum.CODE_100002);
            } else {
                data.setCodeEnum(CodeEnum.CODE_100001);
            }
        }

        Map<String, Object> retMap = new HashMap<>();
        retMap.put("userId", count);
        data.setResults(retMap);

        // 返回数据
        return data;
    }

    /**
     * 修改员工信息
     *
     * @param sysUserInfo
     * @return
     */
    @ApiOperation(value = "修改员工信息", notes = "修改员工信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userName", dataType = "string", required = false, value = "用户真实姓名", defaultValue = "张三丰"),
            @ApiImplicitParam(paramType = "query", name = "subclinicId", dataType = "int", required = false, value = "所属分店ID", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "subclinicName", dataType = "string", required = false, value = "所属分店名称", defaultValue = "顺义分店"),
            @ApiImplicitParam(paramType = "query", name = "userImg", dataType = "string", required = false, value = "职业照", defaultValue = "https://t1.onvshen.com:85/gallery/17596/24921/0.jpg"),
            @ApiImplicitParam(paramType = "query", name = "userRole", dataType = "int", required = false, value = "用户角色/职位", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "userPhone", dataType = "string", required = false, value = "联系电话", defaultValue = "13312341234"),
            @ApiImplicitParam(paramType = "query", name = "userGender", dataType = "int", required = false, value = "用户性别:0:男;1:女", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "userSource", dataType = "int", required = false, value = "员工来源", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "userLevel", dataType = "int", required = false, value = "员工来源", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "int", required = true, value = "要修改员工id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "isWork", dataType = "int", required = true, value = "员工是否接诊", defaultValue = "1")
    })
    @RequestMapping(value = "/modify",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData modify(SysUserInfo sysUserInfo) {

        ResponseData data = new ResponseData();

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{sysUserInfo.getUserId()});

        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        }

        int count = userService.updateSysUser(sysUserInfo);
        if (count < 0) {
            data.setCodeEnum(CodeEnum.CODE_10009);
            return data;
        }

        Map<String, Object> retMap = new HashMap<>();
        data.setResults(retMap);

        // 返回数据
        return data;
    }

    /**
     * 查询员工列表
     *
     * @return
     */
    @ApiOperation(value = "获取员工列表", notes = "员工管理")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userStatus", dataType = "int", required = true, value = "员工状态(0.在职，1.离职、辞退)", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "subclinicId", dataType = "int", required = false, value = "部门ID", defaultValue = "1")
    })
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData selectUserList(Integer userStatus, Integer subclinicId) {

        ResponseData data = new ResponseData();
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{userStatus});

        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        } else {
            List<Map<String, Object>> lists;
            if (userStatus == 0) {
                lists = userService.sysUserList(userStatus, subclinicId);
            } else {
                lists = userService.sysUserList2(userStatus, subclinicId);
            }
            Map<String, Object> retMap = new HashMap<>();
            retMap.put("list", lists);
            data.setResults(retMap);
            // 返回数据
            return data;
        }
    }

    /**
     * 查看员工信息
     *
     * @return
     */
    @ApiOperation(value = "查看员工信息", notes = "查看详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "int", required = true, value = "员工id", defaultValue = "1")
    })
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData userDetail(int userId) {

        ResponseData data = new ResponseData();
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{userId});

        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        } else {
            SysUserInfo userInfo = userService.userDetail(userId);
            data.setResults(userInfo);
            // 返回数据
            return data;
        }
    }

    /**
     * 获取门店列表
     *
     * @return
     */
    @ApiOperation(value = "获取门店列表", notes = "门店")

    @RequestMapping(value = "/shopList",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData selectShopList() {

        ResponseData data = new ResponseData();

        List<TSubclinicInfo> lists;
        lists = userService.sysShopList();
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("list", lists);
        data.setResults(retMap);
        // 返回数据
        return data;
    }

    /**
     * 管理员工在职状态
     *
     * @return
     */
    @ApiOperation(value = "管理员工在职状态", notes = "状态恢复")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "int", required = true, value = "员工id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "userStatus", dataType = "int", required = true, value = "设置员工状态(0:在职;1:离职；2辞退)", defaultValue = "1")
    })
    @RequestMapping(value = "/recovery",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData recoveryUser(Integer userId, Integer userStatus) {
        ResponseData data = new ResponseData();
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{userId, userStatus});

        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(codeEnum);
            return data;
        } else {
            int reUserId = userService.updateStatus(userId, userStatus);
            if(reUserId < 0 ){
                data.setCodeEnum(CodeEnum.CODE_20002);
            }
            // 返回数据
            return data;
        }
    }

    /**
     * 获取员工绩效列表
     *
     * @return
     */
    @ApiOperation(value = "获取员工绩效列表", notes = "获取员工绩效列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "subclinicId", dataType = "int", required = true, value = "门店id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "startDate", dataType = "string", required = true, value = "开始时间", defaultValue = "2018-03-01"),
            @ApiImplicitParam(paramType = "query", name = "endDate", dataType = "string", required = true, value = "结束时间", defaultValue = "2018-03-01"),
            @ApiImplicitParam(paramType = "query", name = "isMain", dataType = "int", required = true, value = "0日，1月", defaultValue = "1")
    })
    @RequestMapping(value = "/selectUserDaySaleList",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData selectUserDaySaleList(Integer subclinicId, String startDate,String endDate,Integer isMain) {
        ResponseData data = new ResponseData();
        Map<String,Object> params = new HashMap<>();
        params.put("subclinicId",subclinicId);
        params.put("startDate",startDate);
        params.put("endDate",endDate);
        params.put("isMain",isMain);
        List<Map<String,Object>> saleList = userService.selectUserSaleList(params);

        data.setResults(saleList);

        // 返回数据
        return data;
    }
    /**
     * 获取员工绩效列表
     *
     * @return
     */
    @ApiOperation(value = "获取员工绩效明细", notes = "获取员工绩效明细")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "int", required = true, value = "用户id", defaultValue = "1"),
    })
    @RequestMapping(value = "/selectUserSaleList",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData selectUserSaleList(Integer userId) {
        ResponseData data = new ResponseData();
        Map<String,Object> params = new HashMap<>();
        params.put("userId",userId);
        List<Map<String,Object>> saleList = userService.selectUserSaleDetail(params);

        data.setResults(saleList);

        // 返回数据
        return data;
    }
    /**
     * 员工评价列表
     *
     * @return
     */
    @ApiOperation(value = "员工评价列表", notes = "员工评价列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", dataType = "int", required = true, value = "用户id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "isReply", dataType = "int", required = true, value = "是否评价：全部不传值，0未评价，1评价", defaultValue = "1"),
    })
    @RequestMapping(value = "/selectUserReplyList",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData selectUserReplyList(Integer userId,Integer isReply) {
        ResponseData data = new ResponseData();
        Map<String,Object> params = new HashMap<>();
        params.put("userId",userId);
        params.put("isReply",isReply);
        List<Map<String,Object>> saleList = userService.selectUserReplyList(params);

        data.setResults(saleList);

        // 返回数据
        return data;
    }
    /**
     * 员工评价详情
     *
     * @return
     */
    @ApiOperation(value = "员工评价详情", notes = "员工评价详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "replyId", dataType = "int", required = true, value = "评价id", defaultValue = "1"),
    })
    @RequestMapping(value = "/selectUserReplyInfo",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData selectUserReplyInfo(Integer replyId) {
        ResponseData data = new ResponseData();
        TReplyInfo saleList = userService.selectUserReplyInfo(replyId);

        data.setResults(saleList);

        // 返回数据
        return data;
    }
    /**
     * 修改员工评价
     *
     * @return
     */
    @ApiOperation(value = "修改员工评价", notes = "修改员工评价")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "replyId", dataType = "int", required = true, value = "评价id", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "replyIntegral", dataType = "int", required = true, value = "有效性，最大：5", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "gentleIntegral", dataType = "int", required = true, value = "亲和力，最大：5", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "serviceIntegral", dataType = "int", required = true, value = "服务态度，最大：5", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "subclinicIntegral", dataType = "int", required = true, value = "分店信息，最大：5", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "replyInfo", dataType = "string", required = true, value = "评价内容", defaultValue = "1"),
    })
    @RequestMapping(value = "/updateUserReplyInfo",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData updateUserReplyInfo(TReplyInfo replyInfo) {
        ResponseData data = new ResponseData();
        Integer count = userService.updateUserReplyInfo(replyInfo);

        if(count == 0){
            data.setCodeEnum(CodeEnum.CODE_60018);
            return data;
        }
        // 返回数据
        return data;
    }
}
